<template>
  <div class="login">
    <el-card class="login_card" :body-style="{ padding: '0px' }">
      <div class="login_title" slot="header">商城管理后台</div>
      <el-form class="form_data" label-width="60px" @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="model.password" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%" type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {
        username: "",
        password: ""
      }
    };
  },
  methods: {
    login() {
      const info = {
        cmd: 1002,
        val: this.model
      };
      this.$axios.post("q.php", info).then(res => {
        if (res.data.result.token) {
          sessionStorage.token = res.data.result.token;
          this.$message({
            type: "success",
            message: "登录成功"
          });
          this.$router.push("/");
        } else {
          this.$message({
            type: "danger",
            message: "账号或密码错误，请重新登录！"
          });
        }
      });
    }
  }
};
</script>

<style lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  background: url(../../assets/bg-login.jpg) no-repeat center;
  background-size: cover;
  position: relative;
  .login_card {
    padding: 20px;
    width: 400px;
    height: 250px;
    position: absolute;
    right: 150px;
    top: 50%;
    transform: translateY(-50%);
    .login_title {
      text-align: center;
      font-size: 23px;
      font-weight: 600;
    }
    .form_data {
      padding: 10px;
      margin: 0 auto;
    }
  }
}
</style>
